/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.unwiredgrain.modeling.sscm.impl;

import com.unwiredgrain.modeling.sscm.SSCMPackage;
import com.unwiredgrain.modeling.sscm.TemplateArgument;
import com.unwiredgrain.modeling.sscm.Template;
import com.unwiredgrain.modeling.sscm.TemplateInstantiation;
import com.unwiredgrain.modeling.sscm.TemplateParameter;

import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Template Instantiation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.TemplateInstantiationImpl#getCppName <em>Cpp Name</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.TemplateInstantiationImpl#getArgument <em>Argument</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.TemplateInstantiationImpl#getSubArguments <em>Sub Arguments</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public abstract class TemplateInstantiationImpl extends EObjectImpl implements TemplateInstantiation {
	/**
	 * The default value of the '{@link #getCppName() <em>Cpp Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCppName()
	 * @generated
	 * @ordered
	 */
	protected static final String CPP_NAME_EDEFAULT = null;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TemplateInstantiationImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return SSCMPackage.Literals.TEMPLATE_INSTANTIATION;
	}

	 /**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated NOT
     */
    public String getCppName() {
            // DONE: implement this method to return the 'Cpp Name' attribute
            // Ensure that you remove @generated or mark it @generated NOT
            // throw new UnsupportedOperationException();
            
            TemplateArgument tat = getArgument();
            
            if (tat == null) {
                    return null;
            }
            else if (tat instanceof TemplateParameter) {
                    
                    TemplateParameter tp = (TemplateParameter)tat;
                    return tp.getName();
            }
            else if (tat instanceof Template) {
                    
                    Template tmpl = (Template)tat;
                    
                    if (tmpl.getTemplateParameters().size() == 0) {
                            return tmpl.getName();
                    }
                    else {
                            
                            EList<TemplateParameter> paramlist = tmpl.getTemplateParameters();
                            EList<? extends TemplateInstantiation> arglist = getSubArguments();
                            
                            StringBuilder sb = new StringBuilder();
                            
                            sb.append(tmpl.getName());
                            sb.append("<");
                            
                            for (int i = 0; i < paramlist.size(); i++) {
                                    
                                    // corresponding argument is not created !!!
                                    if (!(i < arglist.size())) {
                                            sb.append("null");
                                    }
                                    else {
                                            // do parameter - argument check
                                            if (!paramlist.get(i).isRequiredTemplateArgumentType(arglist.get(i))) {
                                                    sb.append("invalid_type");
                                            }
                                            else {
                                                    sb.append(arglist.get(i).getCppName());
                                            }
                                    }
                                    
                                    if (i != paramlist.size() - 1) {
                                            sb.append(", ");
                                    }
                            }
                            
                            if (arglist.size() > paramlist.size()) {
                                    sb.append(", ");
                                    sb.append("too_many_args");
                            }
                            
                            sb.append(" >");
                            
                            return sb.toString();
                    }
            }
                            
            return "ERROR";
    }

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<? extends TemplateInstantiation> getSubArguments() {
		// TODO: implement this method to return the 'Sub Arguments' containment reference list
		// Ensure that you remove @generated or mark it @generated NOT
		// The list is expected to implement org.eclipse.emf.ecore.util.InternalEList and org.eclipse.emf.ecore.EStructuralFeature.Setting
		// so it's likely that an appropriate subclass of org.eclipse.emf.ecore.util.EcoreEList should be used.
		throw new UnsupportedOperationException();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public TemplateArgument getArgument() {
		TemplateArgument argument = basicGetArgument();
		return argument != null && argument.eIsProxy() ? (TemplateArgument)eResolveProxy((InternalEObject)argument) : argument;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public TemplateArgument basicGetArgument() {
		// TODO: implement this method to return the 'Argument' reference
		// -> do not perform proxy resolution
		// Ensure that you remove @generated or mark it @generated NOT
		throw new UnsupportedOperationException();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SSCMPackage.TEMPLATE_INSTANTIATION__SUB_ARGUMENTS:
				return ((InternalEList<?>)getSubArguments()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case SSCMPackage.TEMPLATE_INSTANTIATION__CPP_NAME:
				return getCppName();
			case SSCMPackage.TEMPLATE_INSTANTIATION__ARGUMENT:
				if (resolve) return getArgument();
				return basicGetArgument();
			case SSCMPackage.TEMPLATE_INSTANTIATION__SUB_ARGUMENTS:
				return getSubArguments();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case SSCMPackage.TEMPLATE_INSTANTIATION__CPP_NAME:
				return CPP_NAME_EDEFAULT == null ? getCppName() != null : !CPP_NAME_EDEFAULT.equals(getCppName());
			case SSCMPackage.TEMPLATE_INSTANTIATION__ARGUMENT:
				return basicGetArgument() != null;
			case SSCMPackage.TEMPLATE_INSTANTIATION__SUB_ARGUMENTS:
				return !getSubArguments().isEmpty();
		}
		return super.eIsSet(featureID);
	}

} //TemplateInstantiationImpl
